package com.ml.zookeeper.lock;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

/**
 * 分布式锁验证
 */
public class DistributedLockTest {

    public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
        final DistributedLock lock1 = new DistributedLock();
        final DistributedLock lock2 = new DistributedLock();

        new Thread(() -> {
            try {
                lock1.zkLock("线程1");
                System.out.println("线程1启动，获取到锁");
                Thread.sleep(5 * 1000);

                lock1.unZkLock();
                System.out.println("线程1释放锁");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                lock2.zkLock("线程2");
                System.out.println("线程2启动，获取到锁");
                Thread.sleep(5 * 1000);

                lock2.unZkLock();
                System.out.println("线程2释放锁");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
